package org.hales.manager;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.RequestContextListener;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import org.hales.common.Enum;
@Component
public class RequestManager {
  public static final String CURRENT_USER = "currentUser";
  public static final String CHECK_CODE = "checkCode"; // 验证码
  public static final String CHECK_CODE_VALID_TIME = "checkCodeValidTime"; //验证码失效时间

  @Bean
  public RequestContextListener requestContextListener(){
    return new RequestContextListener();
  }

  private static HttpServletRequest request() {
    ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
    if (servletRequestAttributes == null) {
      return null;
    }
    return servletRequestAttributes.getRequest();
  }

  /**
   * 当前session
   *
   * @return
   */
  public static HttpSession currentSession() {
    HttpServletRequest request = request();
    if (request == null) {
      return null;
    }

    return request.getSession();
  }


  /**
   * 当前ContextPath
   *
   * @return
   */
  public static String currentContextPath() {
    HttpServletRequest request = request();
    if (request == null) {
      return "";
    }

    String path = request.getContextPath();
    if (StringUtils.isBlank(path)) {
      path = "/";
    }
    return path;
  }


  /**
   * 当前访问IP
   *
   * @return
   */
  public static String currentIP() {
    HttpServletRequest request = request();
    if (request == null) {
      return "";
    }

    return request.getRemoteAddr();
  }

  /**
   * 当前访问地址
   *
   * @return
   */
  public static String currentFullURL() {
    HttpServletRequest request = request();
    if (request == null) {
      return "";
    }

    return request.getRequestURL().toString();
  }

  public static String currentURL() {
    HttpServletRequest request = request();
    if (request == null) {
      return "";
    }

    return request.getRequestURL().toString().replace(request.getRequestURI(), "");
  }

  /** 清空session */
  public static void clearSession() {
    HttpSession session = currentSession();
    if (session == null) {
      return;
    }

    session.invalidate();
  }
  /**
   * 设置session属性
   *
   * @param key
   * @param val
   */
  public static void setSession(String key, Object val) {
    HttpSession session = currentSession();
    if (session == null) {
      return;
    }
    session.setAttribute(key, val);
  }

  /**
   * 设置验证码
   *
   * @param checkCode 验证码
   */
  public static void setCheckCode(String checkCode) {
    //在当前会话对象的属性中存储验证码信息
    //存储验证码
    setSession(CHECK_CODE, checkCode);
    //存储验证码验失效时间；证码失效时间为 当前时间+60秒
    setSession(CHECK_CODE_VALID_TIME, new Date((new Date()).getTime() + 60 * 1000));
  }
  /**
   * 校验验证码
   *
   * @param checkCode 验证码
   * @return
   */
  public static Enum.ValidCheckCodeResultType validCheckCode(String checkCode) {
    HttpSession session = currentSession();
    if (session.getAttribute(CHECK_CODE) == null || session.getAttribute(CHECK_CODE_VALID_TIME) == null) {
      return Enum.ValidCheckCodeResultType.CheckCodeWrong;
    }

    //判断验证码是否失效
    Date currentTime = new Date();//当前时间
    if (currentTime.compareTo((Date) session.getAttribute(CHECK_CODE_VALID_TIME)) > 0) {
      return Enum.ValidCheckCodeResultType.CheckCodeExpire;
    }

    //判断验证码是否正确
    if (session.getAttribute(CHECK_CODE).toString().toLowerCase().equals(checkCode.toLowerCase())) {
      //验证成功后，清空验证码和失效时间
      RequestManager.setSession(CHECK_CODE, null);
      RequestManager.setSession(CHECK_CODE_VALID_TIME, null);
      return Enum.ValidCheckCodeResultType.Success;
    } else {
      return Enum.ValidCheckCodeResultType.CheckCodeWrong;
    }
  }

}
